<?php

class CouponApp extends BackendApp {

    var $_coupon_mod;
    var $_couponsn_mod;

    function __construct() {
        $this->CouponApp();
    }

    function CouponApp() {
        parent::BackendApp();
        $this->_coupon_mod = & m('coupon');
        $this->_couponsn_mod = & m('couponsn');
    }

    function index() {

        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ), array(
                'field' => 'coupon.start_time',
                'name' => 'start_time',
                'equal' => '>=',
                'handler' => 'gmstr2time',
            ), array(
                'field' => 'coupon.end_time',
                'name' => 'end_time',
                'equal' => '<=',
                'handler' => 'gmstr2time_end',
            ),
        ));
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc'))) {
                $sort = 'end_time';
                $order = 'asc';
            }
        } else {
            $sort = 'end_time';
            $order = 'asc';
        }

        $page = $this->_get_page(20);
        $coupon_list = $this->_coupon_mod->find(array(
            'fields' => 'coupon.*,s.store_name,s.store_id',
            'conditions' => "1=1 " . $conditions,
            'join' => 'belong_to_store',
            'order' => "$sort $order",
            'limit' => $page['limit'],
            'count' => true,
        ));

        $page['item_count'] = $this->_coupon_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('coupon_list', $coupon_list);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->display('coupon.index.html');
    }
    function drop() {
        $coupon_id = isset($_GET['id']) ? intval($_GET['id']) : '';
        if (empty($coupon_id)) {
            $this->show_warning('no_coupon');
            return;
        }
        $coupon = $this->_coupon_mod->get($coupon_id);
        if (empty($coupon)) {
            $this->show_warning('no_coupon');
            return;
        }
        $this->_coupon_mod->drop($coupon_id);
        if ($this->_coupon_mod->has_error()) {
            $this->show_warning($this->_coupon_mod->get_error());
            return;
        }
        $this->_couponsn_mod->drop('coupon_id =' . $coupon_id);
        $this->show_message('drop_ok', 'back_list', 'index.php?app=coupon');
    }
    
    //异步修改数据
   function ajax_col()
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();
       if (in_array($column ,array('mall_recommended','sort_order')))
       {
           $data[$column] = $value;
           $this->_coupon_mod->edit($id, $data);
           if(!$this->_coupon_mod->has_error())
           {
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }
}

?>
